using Avalonia.Controls;
using Avalonia.Extensions.Controls;
using Avalonia.Extensions.Event;
using Avalonia.Input;
using Avalonia.Interactivity;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.Model.Live;
using Bilibili.WebClient.Api;
using DynamicData;
using PCLUntils.IEnumerables;
using PCLUntils.Json;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Windows
{
    public partial class LiveAreaWindow : WindowBase
    {
        private string sort_type;
        private string AreaId { get; set; }
        private LiveCenterClient Client { get; }
        private string ParentAreaId { get; set; }
        private int CurrentPage { get; set; } = 1;
        private ObservableCollection<NewTags> Tags { get; }
        private ObservableCollection<RoomListItem> Items { get; }
        public LiveAreaWindow()
        {
            InitializeComponent();
            Client = new LiveCenterClient();
            Tags = new ObservableCollection<NewTags>();
            Items = new ObservableCollection<RoomListItem>();
        }
        protected override void AfterInitializeComponent()
        {
            tags.SelectionChanged += Tags_SelectionChanged;
            tags.ItemsSource = Tags;
            listBox.ItemClick += ListBoxOnItemClick;
            listBox.ScrollEnd += ListBoxOnScrollEnd;
            listBox.ItemsSource = Items;
        }
        private void ListBoxOnScrollEnd(object sender, RoutedEventArgs e)
        {
            try
            {
                RunOnUiThread(async () =>
                {
                    if (Items.Count > 0)
                    {
                        CurrentPage++;
                        await LoadData(sort_type);
                    }
                });
            }
            catch { }
        }
        private void ListBoxOnItemClick(object sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickMouse == MouseButton.Left && e.ClickItem is GridViewItem item && item.Content is RoomListItem model)
                    App.Mapper.NavigateToPlayer(model.roomid, PlayMode.Live);
            }
            catch { }
        }
        public override void Callback()
        {
            RunOnUiThread(async () =>
            {
                var data = Data.ToJObject();
                AreaId = data["area_id"].ToString();
                ParentAreaId = data["parent_area_id"].ToString();
                await LoadData();
            });
        }
        private void Tags_SelectionChanged(object? sender, SelectionChangedEventArgs e)
        {
            RunOnUiThread(async () =>
            {
                if (e.AddedItems.FindItem(0) is NewTags tag)
                {
                    CurrentPage = 1;
                    sort_type = tag.sort_type;
                    await LoadData(sort_type);
                }
            });
        }
        private async Task LoadData(string sort = "online")
        {
            try
            {
                var model = await Client.GetRoomList(AreaId, ParentAreaId, CurrentPage, sort);
                if (model.IsSuccess())
                {
                    if (CurrentPage == 1)
                        Items.Clear();
                    Items.AddRange(model.Data.list);
                    if (Tags.Count == 0)
                    {
                        Tags.AddRange(model.Data.new_tags);
                        tags.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("LoadData", ex);
            }
        }
    }
}